// pages/common/pages/orderdetail/orderdetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: null, //订单（保护用户信息和商品信息）
    address:'',
    isShowDialog: false, //是否显示选择商品对话框
    dialogType: "change", //对话框类型： change 修改
    dialogNumValue: 0, //修改购物车商品数量的值
    deteleDialogShow: false, //是否显示删除商品对话框
    isEdit: false,        //是否可编辑，ture则显示修改地址和和商品信息，否则隐藏编辑布局
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    goodsList: [], //商品列表
    delId: 0, //要删除的商品id
    tel:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //接受传递过来的订单
    let order = JSON.parse(options.order);
    this.setData({
      order: order
    });
    if(order.status==0||order.status==2){
      this.setData({
        isEdit: true
      });
    }
    this.queryGoods();
  },
  addressInput(e){
    let address = e.detail;
  
    this.setData({

      address: address,
    })
  },
  telInput(e) {
    let tel = e.detail;
    this.setData({

      tel: tel
    })
  },
  /**
   * 修改地址信息
   */
  changeAddress() {
    
    // wx.chooseAddress({
    //   success(res) {
    //     //获取地址成功
    //     console.log("地址：" + res.userName + " " +
    //       res.provinceName + res.cityName + res.countyName + res.detailInfo + " " +
    //       res.telNumber);
    //   }
    // })
    
   
    
    
    this.goodsEditAddresss(this.data.address); 
  },
  /**
  * 修改电话
  */
  changeTel() {
    
    this.goodsEditTel(this.data.tel); 
  },
  /**
   * 修改商品数量
   */
  change_num: function(e) {
    console.log("========" + e.currentTarget.dataset.item.id);
    this.setData({
      dialogType: "change",
      dialogNumValue: e.currentTarget.dataset.item.buyCount,
      isShowDialog: true,
      goods: e.currentTarget.dataset.item

    })

  },
  /**
   * 删除商品
   */
  delete_goods: function(e) {
    this.setData({
      //显示删除提示对话框
      deteleDialogShow: true,
      delId: e.currentTarget.dataset.item.id
    })
  },
  /**
   * 商品数量对话框取消按钮事件监听
   */
  _cancelEvent(e) {
    console.log('你点击了取消:');
    //do something when cancle is clicked
    this.setData({
      isShowDialog: false
    })
  },
  /**
   * 商品数量对话框确定按钮事件监听
   */
  _confirmEvent(e) {
    console.log('你点击了确定:');
    console.log(e);
    ////do something when sure is clicked
    this.setData({
      isShowDialog: false
    });
    console.log(e.detail.num)
    this.goodsEdit(e.detail.num);
  },
  /**
   * 删除对话框按钮点击
   */
  deteltDialogBtn: function(e) {
    this.setData({
      deteleDialogShow: false
    });
    console.log(e)
    if (e.currentTarget.detail.item.text === "确定") {
      this.delgoods(this.data.delId);
    } else {
      //取消按钮点击
    }
  },
  queryGoods() {
    let _this = this;
    var header = app.globalData.header; //获取app.js中的请求头
    wx.request({
      url: app.globalData.ipPort +'/front/order/query/goods/' + _this.data.order.id,
      method: 'get',
      header: header,
      success(res) {
        if (res.data.code == 200) {
          var data = res.data.data
          _this.setData({
            goodsList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  /**
    * 订单商品修改
    */
  goodsEdit(newCount) {
    var _this = this;
    wx.request({
      url: app.globalData.ipPort +'/front/order/edit',
      method: 'post',
      header: {
        'Cookie': app.globalData.header.Cookie,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: _this.data.goods.id,
        goodsId: _this.data.goods.goodsId,
        orderId:_this.data.order.id,
        buyCount: newCount
      },
      success(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "修改成功",
            icon: "none"
          })
          _this.queryGoods();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }, fail(res) {
       
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  /**
    * 订单地址修改
    */
  goodsEditAddress(address) {
    var _this = this;
    wx.request({
      url: app.globalData.ipPort +'/front/order/edit/address',
      method: 'post',
      header: {
        'Cookie': app.globalData.header.Cookie,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
      
        orderId: _this.data.order.id,
        address: address.value
      },
      success(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "修改成功",
            icon: "none"
          })
          _this.queryGoods();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }, fail(res) {

        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  /**
    * 订单电话修改
    */
  goodsEditTel(tel) {
    var _this = this;
    console.log(tel);
    wx.request({
      url: app.globalData.ipPort +'/front/order/edit/tel',
      method: 'post',
      header: {
        'Cookie': app.globalData.header.Cookie,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        orderId: _this.data.order.id,
        tel: tel.value
      },
      success(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "修改成功",
            icon: "none"
          })
          _this.queryGoods();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }, fail(res) {

        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  /**
   * 删除接口
   */
  delgoods(id) {
    var _this = this;
    console.log("订单商品ID" + id)
    wx.request({
      url: app.globalData.ipPort +'/front/order/goodsorder/delete',
      method: 'post',
      header: {
        'Cookie': app.globalData.header.Cookie,
      },
      data:{
        id:id,
        orderId:_this.data.order.id
      },
      success(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "删除成功",
            icon: "none"
          })
          
          _this.queryGoods();
        } else {
         
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }, fail(res) {

        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
})